package cn.wolfcode.luowowo.website.interceptor;

import cn.wolfcode.luowowo.cache.service.IUesrInfoCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author 猫教授
 * @date 2019/8/8 20:17
 */
//这里我们配置拦截器进行登录拦截校验
@Component
public class CheckLoginInterceptor implements HandlerInterceptor {
    @Reference
    private IUesrInfoCacheService redisCacheService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //如果登录那就直接走
        UserInfo userInfo = redisCacheService.getUserInfo(CookieUtil.getToken(request));
        if (userInfo != null) {
            Cookie faceTime = CookieUtil.cookieContract(request, "faceTime", Consts.USER_INFO_TOKEN_VAI_TIME * 60);
            response.addCookie(faceTime);
            return true;
        }
        if (handler instanceof HandlerMethod) {
            //首先先进行转换
            Method method = ((HandlerMethod) handler).getMethod();
            //再判断是否需要验证如果不需要那么直接放行
            boolean isRequireLogin = method.isAnnotationPresent(RequireLogin.class);
            if (isRequireLogin) {
                boolean isJson = method.isAnnotationPresent(ResponseBody.class);
                //这里要做区分,看是不是ajax请求如果是要以ajax的方式返回给端,如果不是ajax请求.那么再进行页面跳转
                if (isJson) {
                    response.setContentType("text/json;charset=utf-8");
                    response.getWriter().print(JSON.toJSONString(new AjaxResult(false, "必须登录后才能进行操作喔")));
                    return false;
                } else {
                    response.sendRedirect("/login.html");
                    return false;
                }
            }
        }
        return true;
    }

//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
//        System.out.println(response);
//        String token = CookieUtil.getToken(request);
//        UserInfo userInfo = redisCacheService.getUserInfo(token);
//        if (userInfo != null) {
//            //不为空证明有登录或者登录没有过期,毕竟能从后台方法里面出来都是一斤通过了前台拦截器的,
//            //所以这个时候我们只需要判断是否有登录就可以
//            Cookie faceTime = CookieUtil.cookieContract(request, "faceTime", Consts.USER_INFO_TOKEN_VAI_TIME * 60);
//            response.addCookie(faceTime);
//        }
//    }
}
